package cn.galudisu.reactivemanifesto

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.stream.ActorMaterializer
import cn.galudisu.reactivemanifesto.event.{CircuitBreakerBus, Panel}

trait Interrupt {
  private[reactivemanifesto] def choreography: ActorRef
  private[reactivemanifesto] def breaker: ActorRef
}

object Interrupt extends Interrupt {
  private[reactivemanifesto] var choreography: ActorRef = _
  private[reactivemanifesto] var breaker     : ActorRef = _
  def apply()(implicit system: ActorSystem, mat: ActorMaterializer): Interrupt = {
    import cn.galudisu.reactivemanifesto.GospelConfig._

    if(enabled) {
      val eventBus: CircuitBreakerBus = new CircuitBreakerBus(system)
      choreography = system.actorOf(Props(classOf[Choreography]), "choreography")
      breaker = system.actorOf(Props(new GospelMaster(eventBus)), name = "breaker")
      eventBus.subscribe(choreography, classOf[Panel])
    }
    this
  }
}
